<!--发起督办-->
<template>
  <el-dialog append-to-body title="发起督办" width="640px" :visible="visible" @close="handleClose">
    <el-form ref="form" :model="form" :rules="rules" label-suffix="：" class="white-card">
      <el-form-item label="督办类型" prop="publicStatus">
        <el-radio-group>
          <el-radio>全程督办</el-radio>
          <el-radio>重点督办</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="督办备注" prop="comment">
        <el-input v-model="form.comment" show-word-limit maxlength="300" placeholder="请输入详细描述" type="textarea" :autosize="{ minRows: 6 }"></el-input>
      </el-form-item>
    </el-form>
    <div class="split"></div>

    <div class="dialog-footer-btn">
      <el-button @click="handleClose" type="primary" plain>关闭</el-button>
      <el-button @click="handleSubmit" type="primary">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import * as workOrderApi from "@/api/workOrder";
import * as departmentApi from "@/api/department";
import { cancelFinish, distribute } from "../../../api/workOrder";
import * as minix from "@/utils/mixins";

export default {
  name: "SendSupervision",
  mixins: [minix.loading],
  props: ["visible", "workOrderNo"],
  data() {
    return {
      submitting: false,
      departmentTree: [],
      form: {
        acceptDepartmentCode: "",
        comment: "",
      },
      rules: {
        acceptDepartmentCode: [{ required: true, message: "请选择" }],
      },
    };
  },
  watch: {
    visible(val) {
      if (val) {
        // this.getDepartmentTree()
      }
    },
  },
  methods: {
    handleSubmit() {
      this.$refs.form.validate((valid) => {
        if (!valid) return;
        this.decorateLoading(() => {
          const form = { ...this.form };
          form.workOrderNo = this.workOrderNo;
          // form.acceptDepartment = this.findDepartmentName(form.acceptDepartmentCode)
          return workOrderApi.cancelFinish(form).then((res) => {
            this.$emit("success");
            this.handleClose();
          });
        }, "submitting");
      });
    },
    handleClose() {
      this.$refs.form.resetFields();
      this.$emit("update:visible", false);
    },
    findDepartmentName(code) {
      let name = "";
      const find = (list) => {
        for (const item of list) {
          if (item.code === code) {
            name = item.name;
            return;
          } else if (item.children && item.children.length > 0) {
            find(item.children);
          }
        }
      };
      find(this.departmentTree);
      return name;
    },
    getDepartmentTree() {
      departmentApi.tree().then((res) => {
        const removeEmptyChildren = (list) => {
          for (const item of list) {
            if (item.children && item.children.length > 0) {
              removeEmptyChildren(item.children);
            } else {
              delete item.children;
            }
          }
        };
        removeEmptyChildren(res.result);
        this.departmentTree = res.result;
      });
    },
  },
};
</script>

<style scoped></style>
